package com.jaydenxiao.common.actionUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.jaydenxiao.common.baseapp.BaseApplication;


/**
 * 对SharedPreference文件中的各种类型的数据进行存取操作
 *
 */
public class BaseSPUtils {

    private static SharedPreferences sp;

    public static final String COOKIE="cookie";
    public static final String IS_LOGIN="isLogin";

    public static String PHONE ="PHONE";
    public static String HEAD_IMG ="head_img";

    public static String LONGITUDE="longitude";
    public static String LATITUDE="latitude";
    public static String USER_ID="USER_ID";
    public static String JPUSH_ID="JPUSH_ID";

    public static String MODEL_TYPE="model_type";
    public static String SERIAL_NUMBER="serial_number";
    public static String MAC="mac";
    public static String IP="ip";
    public static String IS_ROOT="is_root";
    public static String NET_WORK_TYPE_NAME="netWorkTypeName";
    public static String PROVIDERS_NAME="providersName";
    public static String ACTION_URL="action_url";

    private static void init(Context context) {
        if (sp == null) {
            sp = PreferenceManager.getDefaultSharedPreferences(BaseApplication.getAppContext());
        }
    }

    public static void setSharedIntData(Context context, String key, int value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getSharedIntData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getInt(key, 0);
    }

    public static int getSharedIntData(String key) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        return sp.getInt(key, 0);
    }

    public static void setSharedlongData(Context context, String key, long value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putLong(key, value).commit();
    }

    public static long getSharedlongData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getLong(key, 0l);
    }

    public static void setSharedFloatData(Context context, String key,
                                          float value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putFloat(key, value).commit();
    }

    public static Float getSharedFloatData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getFloat(key, 0f);
    }

    public static Float getSharedFloatData(String key) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        return sp.getFloat(key, 0f);
    }

    public static void setSharedBooleanData(Context context, String key,
                                            boolean value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putBoolean(key, value).commit();
    }

    public static Boolean getSharedBooleanData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getBoolean(key, false);
    }

    public static Boolean getSharedBooleanData(String key) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        return sp.getBoolean(key, false);
    }

    public static void setSharedStringData(Context context, String key, String value) {
        if (sp == null) {
            init(context);
        }
        sp.edit().putString(key, value).commit();
    }

    public static void setSharedStringData(String key, String value) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getSharedStringData(Context context, String key) {
        if (sp == null) {
            init(context);
        }
        return sp.getString(key, "");
    }

    public static String getSharedStringData(String key) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        return sp.getString(key, "");
    }


    public static void clearData(){
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        sp.edit().clear().commit();
    }

    public static String getStringWithDefValue(String key, String defaultValue) {
        if (sp == null) {
            init(BaseApplication.getAppContext());
        }
        return sp.getString(key, defaultValue);
    }

}